package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.application.annotation.DataScope;
import com.wlyuan.open.scanner.dao.entity.CustomerDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-09-22
 */
public interface CustomerMapper extends BaseMapper<CustomerDO> {

    String QUERY = "SELECT Customer_ID, Customer_GUID, opt_status, Customer_OwnerCompany, Customer_CompanyID, Customer_ClientCode, Customer_Name, Customer_Industry, Customer_Web, Customer_ShortName, Customer_EnName, Customer_ShortEnName, Customer_Master, Customer_License, Customer_Contact, Customer_Phone, Customer_Fax, Customer_Mail, Customer_Address, Customer_Zip, Customer_WeiXin, Customer_Logo, Customer_Bank, Customer_BankAccount, SUBSTRING(Customer_Description,0,100) as customerDescription, Customer_Creator, Customer_InsertTime, Customer_Updater, Customer_UpdateTime, Customer_Invalid, Customer_Comments, Customer_AutoBalance, Customer_UnderLine, Customer_HotCount " +
            "FROM TMS_MCustomer ";

    String QUERY_TOP = "SELECT top 1 Customer_ID, Customer_GUID, opt_status, Customer_OwnerCompany, Customer_CompanyID, Customer_ClientCode, Customer_Name, Customer_Industry, Customer_Web, Customer_ShortName, Customer_EnName, Customer_ShortEnName, Customer_Master, Customer_License, Customer_Contact, Customer_Phone, Customer_Fax, Customer_Mail, Customer_Address, Customer_Zip, Customer_WeiXin, Customer_Logo, Customer_Bank, Customer_BankAccount, SUBSTRING(Customer_Description,0,100) as customerDescription, Customer_Creator, Customer_InsertTime, Customer_Updater, Customer_UpdateTime, Customer_Invalid, Customer_Comments, Customer_AutoBalance, Customer_UnderLine, Customer_HotCount " +
            "FROM TMS_MCustomer ";

    String WHERE = "WHERE Customer_OwnerCompany > 0";


    /**
     * 查询客户
     *
     * @return
     */
    @Select(QUERY + WHERE)
    @DataScope(column = "Customer_OwnerCompany")
    List<CustomerDO> list();

    /**
     * 根据id获取信息
     *
     * @param id
     * @return
     */
    @Select(QUERY + "WHERE Customer_ID = #{id}")
    CustomerDO getById(Long id);

    @Select(QUERY + "WHERE Customer_OwnerCompany = #{tenantId}")
    List<CustomerDO> getByTenantId(Long tenantId);

    @Select(QUERY_TOP + "WHERE Customer_OwnerCompany = #{ownerCompany} and Customer_CompanyID = #{companyId} and Customer_Invalid = 0")
    CustomerDO getByCompanyId(@Param("ownerCompany") Long ownerCompany, @Param("companyId") Long companyId);

    @Select("SELECT Customer_ID FROM TMS_MCustomer WHERE Customer_InsertTime >= #{startTime} and Customer_InsertTime <= #{endTime}")
    List<Long> getByTime(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT Customer_ID FROM TMS_MCustomer WHERE Customer_OwnerCompany = #{tenantId} and Customer_InsertTime >= #{startTime} and Customer_InsertTime <= #{endTime}")
    List<Long> getByTenantIdTime(@Param("tenantId") Long tenantId, @Param("startTime") String startTime, @Param("endTime") String endTime);

}
